import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Calc extends JFrame {
    public static JButton btn;
    public static JComboBox<String> op;
    public static JTextField[] tfs;
    public Calc(String title){
        super(title);
    }
    public static void main(String[] args){
        Calc cf = new Calc("计算器");
        cf.setSize(700, 200);
        cf.setLayout(new FlowLayout());
        cf.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        JTextField[] tfs = new JTextField[3];
        Calc.tfs = tfs;
        for(int i = 0;i < 3;i++){
            tfs[i] = new JTextField(15);
            cf.add(tfs[i]);
        }
        tfs[2].setEditable(false);
        
        JComboBox<String> op = new JComboBox<>(new String[] {"+", "-", "*", "/", "%"});
        Calc.op = op;
        cf.add(op, 1);

        JButton btn = new JButton("=");
        Calc.btn = btn;
        btn.addActionListener(new Listener());
        cf.add(btn, 3);
        
        cf.setVisible(true);
    }
}

class Listener implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent event) {
        try {
            double n1 = Double.parseDouble(Calc.tfs[0].getText());
            double n2 = Double.parseDouble(Calc.tfs[1].getText());
            double result;
            switch ((String)Calc.op.getSelectedItem()) {
                case "+":
                    result = n1 + n2;
                    break;
                case "-":
                    result = n1 - n2;
                    break;
                case "*":
                    result = n1 * n2;
                    break;
                case "/":
                    if(n2 == 0) {
                        JOptionPane.showMessageDialog(Calc.btn, "除数不能为0");
                        return;
                    }
                    result = n1 / n2;
                    break;
                default:
                    result = n1 % n2;
                    break;
            }
            Calc.tfs[2].setText(result+"");
        } catch (NumberFormatException e){
            JOptionPane.showMessageDialog(Calc.btn, "无效的输入" + e.getMessage());
        }
    }
}